<?php
declare (strict_types=1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Community extends AppModel
{
    //
    public function samples()
    {
        return $this->belongsToMany(Sample::class, 'sample_community', 'sample_id', 'community_id');
    }

    /**
     * @param $community_id
     * @param $review_project_id
     * @User: 刘海龙
     * @Date: 2021/7/27
     * @Time: 11:00
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 根据社区返回对应的样本点
     */
    public function selectSample($community_id, $review_project_id)
    {
        $commit = $this->find($community_id);

        if (is_null($commit)) {
            return json(['code' => 403, 'message' => '不存在']);
        }
        $res = $commit->samples->each(function ($item, $key) use ($review_project_id) {
            if ($item['review_project_id'] == $review_project_id) {
                $item['is_exist'] = true;
            } else {
                $item['is_exist'] = false;
            }
            return $item;
        })->filter(function ($data) {
            if ($data['is_exist']) {
                return true;
            }
        })->toArray();
        $ids = [];
        foreach ($res as $key => $v) {
            $ids[] = $v['id'];
        }
        return $ids;
    }

}
